package com.android.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.calculator.util.Arith;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_result;     // 结果框
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_result = findViewById(R.id.tv_result);
        // 文本移动方式为滚动
        tv_result.setMovementMethod(new ScrollingMovementMethod());

        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_divide).setOnClickListener(this);
        findViewById(R.id.btn_multiply).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_plus).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_minus).setOnClickListener(this);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_dot).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);
        findViewById(R.id.ib_sqrt).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String inputText;
        int resid = v.getId();

        // 根号
        if (resid == R.id.ib_sqrt) {
            inputText = "√";
        } else {
            inputText = ((TextView) v).getText().toString();
        }
        Log.d(TAG, "resid = " + resid + " , inputText=" + inputText);


        if (resid == R.id.btn_clear) {
            // C
            clear("");
            Toast.makeText(this, "归零", Toast.LENGTH_SHORT).show();
        } else if (resid == R.id.btn_cancel) {
            // CE
            if (operator.equals("")) {
                // 没有操作符，逐位取消前一个操作数
                if (firstNum.length() == 1) {
                    firstNum = "0";
                } else if (firstNum.length() > 0) {
                    firstNum = firstNum.substring(0, firstNum.length() - 1);
                } else {
                    Toast.makeText(this, "没有可以取消的数字", Toast.LENGTH_SHORT).show();
                    return;
                }
                showText = firstNum;
            } else {
                // 有操作符，逐位取消后一个数字
                if (nextNum.length() == 1) {
                    nextNum = "";
                } else if (nextNum.length() > 0) {
                    nextNum = nextNum.substring(0, nextNum.length() - 1);
                } else {
                    Toast.makeText(this, "运算符不可取消", Toast.LENGTH_SHORT).show();
                    return;
                }
                showText = showText.substring(0, showText.length() - 1);
            }
            tv_result.setText(showText);
        } else if (resid == R.id.btn_equal) {
            // =
            if (operator.length() == 0 || operator.equals("＝")) {
                Toast.makeText(this, "请输入运算符", Toast.LENGTH_SHORT).show();
                return;
            } else if (nextNum.length() <= 0) {
                Toast.makeText(this, "要输入第二个数才能运算", Toast.LENGTH_SHORT).show();
                return;
            }
            if (calculate()) {
                operator = inputText;
                showText = showText + "=" + result;
                tv_result.setText(showText);
            } else {
                return;
            }
        } else if (resid == R.id.btn_plus || resid == R.id.btn_minus || resid == R.id.btn_multiply || resid == R.id.btn_divide) {
            // +-*/
            if (firstNum.length() <= 0) {
                Toast.makeText(this, "先输入数字再输入运算符", Toast.LENGTH_SHORT).show();
                return;
            }
            if (operator.length() == 0 || operator.equals("＝") || operator.equals("√")) {
                operator = inputText;
                showText += operator;
                tv_result.setText(showText);
            } else {
                Toast.makeText(this, "不支持连续运算，请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
        } else if (resid == R.id.ib_sqrt) {
            // √
            if (firstNum.length() <= 0) {
                Toast.makeText(this, "先输入数字才能开根号", Toast.LENGTH_SHORT).show();
                return;
            }
            if (Double.parseDouble(firstNum) < 0) {
                Toast.makeText(this, "开根号的数值不能为负数", Toast.LENGTH_SHORT).show();
                return;
            }
            result = String.valueOf(Math.sqrt(Double.parseDouble(firstNum)));
            firstNum = result;
            nextNum = "";
            operator = inputText;
            showText = showText + "√=" + result;
            tv_result.setText(showText);
            Log.d(TAG, "result = " + result + " ,firstNum = " + firstNum + " ,operator = " + operator);
        } else {
            // 数字和小数点
            if (operator.equals("＝")) {
                operator = "";
                firstNum = "";
                showText = "";
            }
            if (resid == R.id.btn_dot) {
                inputText = ".";
            }
            if (operator.equals("")){
                if(firstNum.contains(".") && inputText.equals(".")){
                    return;
                }
                firstNum += inputText;
            }else {
                if(nextNum.contains(".") && inputText.equals(".")){
                    return;
                }
                nextNum += inputText;
            }
            showText += inputText;
            tv_result.setText(showText);
        }
        return;
    }

    private boolean calculate() {
        switch (operator) {
            case "＋":
                result = Arith.add(firstNum, nextNum);
                break;
            case "－":
                result = Arith.sub(firstNum, nextNum);
                break;
            case "×":
                result = Arith.mul(firstNum, nextNum);
                break;
            case "÷":
                if (Double.parseDouble(nextNum) == 0) {
                    Toast.makeText(this, "被除数不能为0", Toast.LENGTH_SHORT).show();
                    return false;
                } else {
                    result = Arith.div(firstNum, nextNum);
                }
                break;
        }
        Log.d(TAG, "result = " + result);
        firstNum = result;
        nextNum = "";
        return true;
    }

    private String showText = "";   // 显示的文本内容
    private String firstNum = "";   // 前一个操作数
    private String nextNum = "";    // 后一个操作数
    private String result = "";     // 当前计算结果
    private String operator = "";   // 操作符

    private void clear(String text) {
        showText = text;
        tv_result.setText(showText);
        operator = "";
        firstNum = "";
        nextNum = "";
        result = "";
    }
}
